//
//  DYYuyueTimeCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/5.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit

class DYYuyueTimeCell: SYBaseCell , UICollectionViewDelegate, UICollectionViewDataSource{
    
    var models: [DYJQYuyueTimeModel] = []
    var collectionView: UICollectionView!
    var lastIndexPath:IndexPath!

    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0x367FF6)
        v.layer.cornerRadius = 2
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1E1E1E)
        v.font = 17.autoBoldFontSize()
        v.text = "选择日期"
        return v
    }()
    
    let chakanAbove: UIButton = {
        let v: UIButton  = UIButton()
        return v
    }()
    let chakan: UIImageView = {
        let v: UIImageView = UIImageView()
        v.backgroundColor = .white
        v.image = UIImage(named: "kuang")
        v.layer.cornerRadius = 5
        v.clipsToBounds = true
        return v
    }()
    
    let moreBt: UIButton = {
        let v: UIButton  = UIButton()
        return v
    }()
    let moreLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 14.autoFontSize()
        v.text = "更多\n日期"
        v.numberOfLines = 2
        return v
    }()
    
    var moreBgView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(hex: 0xE6F1FF)
        return v
    }()
    
    let arrow: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "people_arrow")
        return v
    }()
    public func configureView(_ models: [DYJQYuyueTimeModel]) {
        self.models = models
        self.collectionView.reloadData()
    }
    
    override func initViews() {
        
        setCollectionView()
        contentView.addSubview(nameLb)
        contentView.addSubview(bgView)
        contentView.addSubview(collectionView)
        contentView.addSubview(chakan)
        contentView.addSubview(chakanAbove)
        contentView.addSubview(moreLb)
        chakan.addSubview(moreBgView)
        contentView.addSubview(arrow)
        contentView.addSubview(moreBt)
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(14.5))
            make.width.equalTo(4.0.autoWidth())
            make.height.equalTo(15.0.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(bgView)
            make.left.equalTo(bgView.snp.right).offset(autoWidth(7.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(16.0.autoWidth())
        }
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(nameLb.snp.bottom).offset(autoWidth(14.0))
            make.left.equalToSuperview().offset(15.0.autoWidth())
            make.right.equalToSuperview().offset(-78.0.autoWidth())
            make.height.equalTo(58.0.autoWidth())
            make.bottom.equalToSuperview()
        }
        chakan.snp.makeConstraints { (make) in
            make.centerY.equalTo(collectionView)
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(58.0.autoWidth())
            make.width.equalTo(63.0.autoWidth())
        }
        chakanAbove.snp.makeConstraints { (make) in
                make.centerY.equalTo(collectionView)
                make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(58.0.autoWidth())
            make.width.equalTo(63.0.autoWidth())
            }
        moreLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(chakan)
            make.left.equalTo(chakan.snp.left).offset(autoWidth(10.0))
            make.width.height.equalTo(40.0.autoWidth())
        }
        moreBt.snp.makeConstraints { (make) in
            make.centerY.equalTo(chakan)
            make.left.equalTo(chakan.snp.left).offset(autoWidth(10.0))
            make.width.height.equalTo(40.0.autoWidth())
        }
        moreBgView.snp.makeConstraints { (make) in
            make.centerY.equalTo(chakan)
            make.right.equalTo(chakan)
            make.height.equalTo(58.0.autoWidth())
            make.width.equalTo(17.0.autoWidth())
        }
        arrow.snp.makeConstraints { (make) in
            make.centerY.equalTo(chakan)
            make.left.equalTo(moreLb.snp.right).offset(autoWidth(1.0))
            make.width.equalTo(8.0.autoWidth())
            make.height.equalTo(14.0.autoWidth())
        }
    }
    
    func setCollectionView() {
        
            
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        let itemWidth = 88.0.autoWidth()
        layout.itemSize = CGSize(width: itemWidth, height: 58.0.autoWidth())
        layout.minimumLineSpacing = 6
//        layout.minimumInteritemSpacing = 10
        layout.scrollDirection = .horizontal
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(DYYuyueTimeSubCell.self, forCellWithReuseIdentifier: NSStringFromClass(DYYuyueTimeSubCell.self))
        self.addSubview(self.collectionView)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return models.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: DYYuyueTimeSubCell? = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(DYYuyueTimeSubCell.self), for: indexPath) as? DYYuyueTimeSubCell
        if self.models.count > indexPath.row {
            if nil != lastIndexPath, lastIndexPath.row == indexPath.row {
                cell?.pic.image = UIImage(named: "kuang_select")
            } else {
                cell?.pic.image = UIImage(named: "kuang")
            }
            cell?.configureView(self.models[indexPath.row])
        }
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell: DYYuyueTimeSubCell = collectionView.cellForItem(at: indexPath) as! DYYuyueTimeSubCell
        cell.pic.image = UIImage(named: "kuang_select")
        if let i = lastIndexPath {
            if indexPath.row != lastIndexPath.row {
                if let c: DYYuyueTimeSubCell = collectionView.cellForItem(at: i) as? DYYuyueTimeSubCell {
                    c.pic.image = UIImage(named: "kuang")
                }
            }
            lastIndexPath.row = indexPath.row
        } else {
            lastIndexPath = IndexPath(item: indexPath.row, section: 0)
        }
        self.clickedClosure?(indexPath.row)
    }
}

